package com.chansos.dubbogo.hessian_generator;

import com.chansos.dubbogo.common.Constant;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.IPopupChooserBuilder;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.psi.PsiFile;

import java.util.ArrayList;
import java.util.List;

public class HessianGeneratorAction extends AnAction {
    @Override
    public void update(AnActionEvent e) {
        e.getPresentation().setEnabled(false);
        PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
        if (psiFile == null) {
            return;
        }
        e.getPresentation().setEnabled(Constant.GoFileExternal.equals(psiFile.getVirtualFile().getExtension()));
    }

    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
        assert psiFile != null;
        String classPath = psiFile.getVirtualFile().getPath();

        Editor editor = e.getData(CommonDataKeys.EDITOR);
        assert editor != null;
        Caret primaryCaret = editor.getCaretModel().getCurrentCaret();

        int currentLine = editor.getDocument().getLineNumber(primaryCaret.getSelectionStart());
        FileInfo fileInfo = readFile(classPath, currentLine);
        if (fileInfo == null) {
            Messages.showErrorDialog(Constant.ErrorFileReadFail, Constant.ErrorMessageTitle);
            return;
        }
        showMenu(project, fileInfo);
    }

    private FileInfo readFile(String classPath, int currentLine) {
        return FileReader.read(classPath, currentLine);
    }

    private void showMenu(Project project, FileInfo fileInfo) {
        List<String> menu = new ArrayList<>();
        menu.add(Constant.MenuGenerateAll);
        String defaultStructName = fileInfo.getDefaultStructName();
        if (defaultStructName != null && defaultStructName.length() > 0) {
            menu.add(Constant.MenuGenerateOnly + "`" + defaultStructName + "`");
        }
        JBPopupFactory instance = JBPopupFactory.getInstance();
        IPopupChooserBuilder<String> builder = instance.createPopupChooserBuilder(menu);
        builder.setTitle(Constant.MenuGenerateTitle).setItemChosenCallback(selection -> generate(fileInfo, selection));
        builder.createPopup().showCenteredInCurrentWindow(project);
    }

    private void generate(FileInfo fileInfo, String selection) {
        if (!Constant.MenuGenerateAll.equals(selection)) {
            fileInfo.keepOnlyStruct(fileInfo.getDefaultStructName());
        }
        Generator.generate(fileInfo);
        try {
            FileWriter.write(fileInfo);
        } catch (Exception ex) {
            Messages.showErrorDialog(Constant.ErrorFileWriteFail, Constant.ErrorMessageTitle);
        }
    }
}
